package com.sky.service.impl;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.constant.MessageConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.SetmealDTO;
import com.sky.dto.SetmealPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.entity.Setmeal;
import com.sky.entity.SetmealDish;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.DishMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.SetmealService;
import com.sky.vo.DishItemVO;
import com.sky.vo.SetmealVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import java.util.ArrayList;
import java.util.List;


/**
 * @author 西门凌兰
 * @version 1.0
 */
@Service
public class SetmealServiceImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Autowired
    private DishMapper dishMapper;

    /**
     * 根据id查询套餐
     *
     * @param id
     * @return
     */
    @Override
    public SetmealVO selectSetmealWithDishById(Long id) {
        SetmealVO setmealVO = setmealMapper.selectSetmealById(id);
        List<SetmealDish> setmealDishes = setmealDishMapper.getBySetmealId(id);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    /**
     * 套餐的分页查询
     *
     * @param setmealPageQueryDTO
     * @return
     */
    @Override
    public PageResult page(SetmealPageQueryDTO setmealPageQueryDTO) {
        //开始分页查询
        PageHelper.startPage(setmealPageQueryDTO.getPage(), setmealPageQueryDTO.getPageSize());
        Page<Setmeal> page = setmealMapper.page(setmealPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 套餐以及对应的菜品的添加
     *
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void saveSetmealWithDish(SetmealDTO setmealDTO) {
        //套餐的添加
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.insert(setmeal);

        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //给每一个菜品数据的套餐id赋值
        if (setmealDishes != null && setmealDishes.size() > 0) {
            for (SetmealDish setmealDish : setmealDishes) {
                setmealDish.setSetmealId(setmeal.getId());
            }
        }
        //菜品的添加
        setmealDishMapper.inserts(setmealDishes);
    }

    /**
     * 批量删除套餐
     *
     * @param ids
     */
    @Transactional
    @Override
    public void delete(List<Long> ids) {
        //判断套餐是否在售卖,如果在售卖则不能删除
        for (Long id : ids) {
            SetmealVO setmealVO = setmealMapper.selectSetmealById(id);
            if (setmealVO.getStatus() == StatusConstant.ENABLE) {
                throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ON_SALE);
            }
        }
        //删除套餐
        setmealMapper.delete(ids);
        //删除该套餐下对应的菜品
        setmealDishMapper.deleteBySetmealIds(ids);
    }

    /**
     * 修改套餐
     *
     * @param setmealDTO
     */
    @Transactional
    @Override
    public void updateSetmealWithDish(SetmealDTO setmealDTO) {
        //修改套餐
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO, setmeal);
        setmealMapper.update(setmeal);

        //修改套餐关联的菜品
        //1.先删除和该套餐有关的所有菜品
        ArrayList<Long> arrayList = new ArrayList<>();
        arrayList.add(setmeal.getId());
        setmealDishMapper.deleteBySetmealIds(arrayList);
        //2.再添加菜品
        List<SetmealDish> setmealDishes = setmealDTO.getSetmealDishes();
        //为所有菜品添加套餐ID
        for (SetmealDish setmealDish : setmealDishes) {
            setmealDish.setSetmealId(setmealDTO.getId());
        }
        //添加菜品
        setmealDishMapper.inserts(setmealDishes);
    }

    /**
     * 套餐起售停售
     *
     * @param status
     * @param id
     */
    @Override
    public void startOrStop(Integer status, Long id) {
        Setmeal setmeal = new Setmeal();
        setmeal.setId(id);
        setmeal.setStatus(status);
//        if (status == StatusConstant.DISABLE) {
//            setmealMapper.update(setmeal);
//        } else if (status == StatusConstant.ENABLE) {
//            List<Dish> dishes = dishMapper.list(id);
//            for (Dish dish : dishes) {
//                if (dish.getStatus() == StatusConstant.DISABLE) {
//                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
//                }
//            }
//            setmealMapper.update(setmeal);
//        }
        if (status == StatusConstant.ENABLE) {
            List<Dish> dishes = dishMapper.list(id);
            for (Dish dish : dishes) {
                if (dish.getStatus() == StatusConstant.DISABLE) {
                    throw new DeletionNotAllowedException(MessageConstant.SETMEAL_ENABLE_FAILED);
                }
            }
        }
        setmealMapper.update(setmeal);
    }


    /**
     * 根据分类id查询套餐
     * @return
     */
    @Override
    public List<Setmeal> selectSetmealByCategoryId(Long categoryId) {
        List<Setmeal> setmeals = setmealMapper.selectSetmealByCategoryId(categoryId);
        return setmeals;
    }

    /**
     * 根据套餐id查询菜品
     * @param setmealId
     * @return
     */
    @Override
    public List<DishItemVO> selectDishBySetmealId(Long setmealId) {
        List<DishItemVO> dishItemVOS =  setmealMapper.selectDishBySetmealId(setmealId);
        return dishItemVOS;
    }
}
